package com.dongdongshop.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description
 * @ClassName SellerShiroController
 * @Author RC
 * @date 2020.12.13 12:14
 */
@Configuration
public class UserShiroConfig {
    //创建 ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Autowired DefaultWebSecurityManager getDefaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        //设置路径的过滤
        //配置shiro的内置过滤器
        /**
         * Shiro内置过滤器,可以实现权限相关的拦截器
         * anon 无需认证 就可以访问
         * authc  必须认证才可以访问
         * user 如果使用rememberMe的功能可以直接访问
         * perms 该资源必须得到权限才可以访问
         * role 该资源必须得到角色权限才可以访问
         */
        shiroFilterFactoryBean.setSecurityManager(getDefaultWebSecurityManager);
        Map<String,String> filterMap = new LinkedHashMap<>();
        //选择放过
        /*filterMap.put("/**","anon");*/
        filterMap.put("/","anon");
        filterMap.put("/css/**","anon");
        filterMap.put("/data/**","anon");
        filterMap.put("/fonts/**","anon");
        filterMap.put("/js/**","anon");
        filterMap.put("/plugins/**","anon");


        //退出登录
        filterMap.put("/auth/logout","logout");
        //拦截所有
        /*filterMap.put("/**","authc");*/
        //自定义登录页
        shiroFilterFactoryBean.setLoginUrl("/toLogin");
        //设置无权限页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");

        //将过滤放入过滤链中
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }
    //创建DefaultWebSecurityManager
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Autowired UserLoginRealm loginRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(loginRealm);
        return securityManager;
    }
    //创建Realm
    @Bean
    public UserLoginRealm loginRealm(@Autowired HashedCredentialsMatcher hashedCredentialsMatcher){
        UserLoginRealm userLoginRealm = new UserLoginRealm();
        //给realm对象设置加密方式
        userLoginRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return userLoginRealm;
    }
    //加密配置
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        //创建哈希对象
        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        //约定哈希加密算法 有很多种: MD4 MD5 SHA-1 SHA-2
        matcher.setHashAlgorithmName("MD5");
        //盐值加密 定义撒盐次数
        matcher.setHashIterations(3);
        //false : base64 编码方式
        //true : HEX编码
        matcher.setStoredCredentialsHexEncoded(true);//设置加密的编码
        return matcher;
    }
    //shiro整合Thymeleaf 模板引擎
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
}
